//
//  BaseTabBarVC.m
//  LoveFreshBeen
//
//  Created by Cyan on 2016/10/15.
//  Copyright © 2016年 魏根. All rights reserved.
//

#import "BaseTabBarVC.h"
#import "HomeViewController.h"
#import "SupermarketViewController.h"
#import "ShoppingCartViewController.h"
#import "ProfileBigController.h"
#import "CyButton.h"

@interface BaseTabBarVC ()<UITabBarControllerDelegate>

@property(nonatomic,weak) UINavigationController* shopCartNav;

@end

@implementation BaseTabBarVC

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self loadChildViewControllers];
    }
    return self;
}

- (void)loadChildViewControllers {

    // 首页
    UINavigationController *nav = [self creatNavVCWithViewController:[[HomeViewController alloc] init] TabBarImage:[UIImage imageNamed:@"v2_home"] Title:@"主页" selectedIamge:[UIImage imageNamed:@"v2_home_r"] tag:0];
    
    // 闪电超市
    UINavigationController *nav2 = [self creatNavVCWithViewController:[[SupermarketViewController alloc] init] TabBarImage:[UIImage imageNamed:@"v2_order"] Title:@"闪送超市" selectedIamge:[UIImage imageNamed:@"v2_order_r"] tag:1];
    
    // 购物车
    UINavigationController *nav3 = [self creatNavVCWithViewController:[[ShoppingCartViewController alloc] init] TabBarImage:[UIImage imageNamed:@"shopCart"] Title:@"购物车" selectedIamge:[UIImage imageNamed:@"shopCart_r"] tag:2];
    self.shopCartNav = nav3;
    
    // 我的
    UINavigationController *nav4 = [self creatNavVCWithViewController:[[ProfileBigController alloc] init] TabBarImage:[UIImage imageNamed:@"v2_my"] Title:@"我的" selectedIamge:[UIImage imageNamed:@"v2_my_r"] tag:3];

    
    self.viewControllers = @[nav,nav2,nav3,nav4];
    self.delegate = self;
}


- (UINavigationController *)creatNavVCWithViewController:(UIViewController *)viewController TabBarImage:(UIImage *)image Title:(NSString *)title selectedIamge:(UIImage *)selectedIamge tag:(NSInteger)tag{
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
    viewController.title = title;
    
    viewController.tabBarItem.selectedImage = [selectedIamge imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    viewController.tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.tag = tag;
    
    self.tabBar.tintColor = [UIColor darkGrayColor];
    
 
    return nav;
}




- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    if (item.tag == 2) {
        ShoppingCartViewController* carVC = [[ShoppingCartViewController alloc] init];
        UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:carVC];
        [self presentViewController:nav animated:YES completion:nil];
    }
    int index = 0;
    
    for (UIView *sub in tabBar.subviews) {
        
        if ([sub isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            if (index == item.tag) {
                
                for (UIView *target in sub.subviews) {
                    if ([target isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
                        target.transform = CGAffineTransformMakeScale(0.4, 0.4);
                        [UIView animateWithDuration:0.6 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:5 options:0 animations:^{
                            target.transform = CGAffineTransformMakeScale(1, 1);
                            
                       } completion:nil];
                    }
                }
            }
            index++;
        }
    }
}
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    if(viewController == self.shopCartNav){
        return NO;
    }
    else
        return YES;
}

@end
